#include "../base.h"

#define LOG_TAG "MyListener"

static jclass getClass(JNIEnv* env,const char * className)
{
	return (*env)->FindClass(env,className);
}

static jclass getMyListenerClass(JNIEnv* env)
{
	return getClass(env,"person/wangchen11/nativeview/MyListener");
}


//创建监听接口
MyListener newMyListener(int funAddr,int dataAddr)
{
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getMyListenerClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID( env,clazz,"<init>","(II)V" );
	jobject obj = (*env)->NewObject(env,clazz,methoID,funAddr,dataAddr);
	RelativeLayout view = (*env)->NewGlobalRef(env,obj);
	(*env)->DeleteLocalRef(env,obj);
	(*env)->DeleteLocalRef(env,clazz);
	return view;
}

//删除
void deleteMyListener(MyListener thiz)
{
	if(thiz==NULL)
	{
		LOGE("deleteMyListener thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	(*env)->DeleteGlobalRef(env,thiz);
}

//创建一个单击回调
MyListener newOnClickListener(OnClickCallback callback,void *data)
{
	return newMyListener((int)callback,(int)data);
}

//创建一个长按回调
MyListener newOnLongClickListener(OnLongClickCallback callback,void *data)
{
	return newMyListener((int)callback,(int)data);
}

//创建一个按键回调
MyListener newOnKeyListener(OnKeyCallback callback,void *data)
{
	return newMyListener((int)callback,(int)data);
}

//创建一个触摸回调
MyListener newOnTouchListener(OnTouchCallback callback,void *data)
{
	return newMyListener((int)callback,(int)data);
}

//创建一个绘图回调
MyListener newOnDrawListener(OnDrawCallback callback,void *data)
{
	return newMyListener((int)callback,(int)data);
}

//创建一个多媒体播放完成回调
MyListener newOnCompletionListener(OnCompletionCallback callback,void *data)
{
	return newMyListener((int)callback,(int)data);
}


//////////////////////////JNI Call//////////////////////////
void Java_person_wangchen11_nativeview_MyListener_onClick
( JNIEnv* env,jclass clazz,jint funAddr,jint dataAddr,View view)
{
	OnClickCallback callback = (OnClickCallback)funAddr;
	void *data = (void*)dataAddr;
	callback(view,data);
}

jboolean Java_person_wangchen11_nativeview_MyListener_onLongClick
( JNIEnv* env,jclass clazz,jint funAddr,jint dataAddr,View view)
{
	OnLongClickCallback callback = (OnLongClickCallback)funAddr;
	void *data = (void*)dataAddr;
	return callback(view,data);
}


jboolean Java_person_wangchen11_nativeview_MyListener_onKey
( JNIEnv* env,jclass clazz,jint funAddr,jint dataAddr,View view,jint key,jobject keyEvent)
{
	OnKeyCallback callback = (OnKeyCallback)funAddr;
	void *data = (void*)dataAddr;
	return callback(view,key,keyEvent,data);
}


jboolean Java_person_wangchen11_nativeview_MyListener_onTouch
( JNIEnv* env,jclass clazz,jint funAddr,jint dataAddr,View view,jint action,jfloat x,jfloat y,jobject event)
{
	OnTouchCallback callback = (OnTouchCallback)funAddr;
	void *data = (void*)dataAddr;
	return callback(view,action,x,y,event,data);
}

void Java_person_wangchen11_nativeview_MyListener_draw
( JNIEnv* env,jclass clazz,jint funAddr,jint dataAddr,View view,Canvas canvas)
{
	OnDrawCallback callback = (OnDrawCallback)funAddr;
	void *data = (void*)dataAddr;
	callback(view,canvas,data);
}

void Java_person_wangchen11_nativeview_MyListener_onCompletion
( JNIEnv* env,jclass clazz,jint funAddr,jint dataAddr,jobject mediaPlayer)
{
	OnCompletionCallback callback = (OnCompletionCallback)funAddr;
	void *data = (void*)dataAddr;
	callback(mediaPlayer,data);
}

